package com.web;

import com.javabean.Book;
import com.javabean.Page;
import com.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientBookServlet extends BaseServlet {

    //分页显示
    public void paging(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求的参数pageNum,pageSize
        String pageNum = request.getParameter("pageNum");
        if (pageNum == null) {
            //2.调用BookService.page(pageNum,pageSize)
            BookServiceImpl a = new BookServiceImpl();
            Page<Book> page = a.page(1, 4);
            //3. 保存数据到request域中
            request.setAttribute("page", page);
            //4.请求转发
            request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
        } else {
            int i = Integer.parseInt(pageNum);
            //2.调用BookService.page(pageNum,pageSize)
            BookServiceImpl a = new BookServiceImpl();
            Page<Book> page = a.page(i, 4);
            //3. 保存数据到request域中
            request.setAttribute("page", page);
            //4.请求转发
            request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
        }
    }

    //跳转到选择页
    public void forpaging(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输入的页号
        String pn = request.getParameter("pn");
        int i = Integer.parseInt(pn);
        BookServiceImpl a = new BookServiceImpl();
        Page<Book> page = a.page(i, 4);
        Integer pageTotal = page.getPageTotal();
        if (1 <= i && i <= pageTotal) {
            //4.请求转发
            //3. 保存数据到request域中
            request.setAttribute("pn", i);
            request.setAttribute("page", page);
            request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
        } else if (i > pageTotal) {
            request.setAttribute("pn", pageTotal);
            page = a.page(pageTotal, 4);
            request.setAttribute("page", page);
            request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
        } else if (i < 1) {
            request.setAttribute("pn", 1);
            page = a.page(1, 4);
            request.setAttribute("page", page);
            request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
        }
    }

    //按照价格查询后分页显示
    public void pagingByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求的参数pageNum,pageSize
        String min = request.getParameter("min");
        String max = request.getParameter("max");
        int p = Integer.parseInt(min);
        int j = Integer.parseInt(max);
        String pageNum = request.getParameter("pageNum");
        //2.调用BookService.page(pageNum,pageSize)
        if (pageNum == null) {
            //2.调用BookService.page(pageNum,pageSize)
            BookServiceImpl a = new BookServiceImpl();
            Page<Book> page = a.pageByPrice(1, 4, p, j);
            //3. 保存数据到request域中
            request.setAttribute("page", page);
            request.setAttribute("min", p);
            request.setAttribute("max", j);
            //4.请求转发
            request.getRequestDispatcher("/pages/client/indexByPrice.jsp").forward(request, response);
        } else {
            int i = Integer.parseInt(pageNum);
            //2.调用BookService.page(pageNum,pageSize)
            BookServiceImpl a = new BookServiceImpl();
            Page<Book> page = a.pageByPrice(i, 4, p, j);
            //3. 保存数据到request域中
            request.setAttribute("page", page);
            request.setAttribute("min", p);
            request.setAttribute("max", j);
            //4.请求转发
            request.getRequestDispatcher("/pages/client/indexByPrice.jsp").forward(request, response);
        }
    }


    //按照价格跳转到选择页
    public void forpagingByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输入的页号
        String pn = request.getParameter("pn");
        int i = Integer.parseInt(pn);
        String min = request.getParameter("min");
        String max = request.getParameter("max");
        int p = Integer.parseInt(min);
        int j = Integer.parseInt(max);
        BookServiceImpl a = new BookServiceImpl();
        Page<Book> page = a.pageByPrice(i, 4, p, j);
        //3. 保存数据到request域中
        request.setAttribute("pn", i);
        request.setAttribute("min", p);
        request.setAttribute("max", j);
        request.setAttribute("page", page);
        Integer pageTotal = page.getPageTotal();
        if (1 <= i && i <= pageTotal) {
            //4.请求转发
            request.getRequestDispatcher("/pages/client/indexByPrice.jsp").forward(request, response);
        } else if (i > pageTotal) {
            request.setAttribute("pn", pageTotal);
            page = a.pageByPrice(pageTotal, 4, p, j);
            request.setAttribute("page", page);
            request.getRequestDispatcher("/pages/client/indexByPrice.jsp").forward(request, response);
        } else if (i < 1) {
            request.setAttribute("pn", 1);
            page = a.pageByPrice(1, 4, p, j);
            request.setAttribute("page", page);
            request.getRequestDispatcher("/pages/client/indexByPrice.jsp").forward(request, response);
        }
    }
}
